package com.zhaoshixin.rabbit.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 作者:赵士欣
 * 日期:2022/7/18
 **/
@Configuration
public class RabbitMQConfig {
    // 1.声明一个业务的交换机 一个
    //业务交换机
    @Bean
    public FanoutExchange bizExchange(){

        return new FanoutExchange("bizExchange");
    }

    //2.声明队列 二个
    //业务队列1
    @Bean
    public Queue bizQueue1(){
        //队列1和死信交换机绑定
        Map<String, Object> args = new HashMap<>();
//       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
//        args.put("x-dead-letter-exchange", "deadLetterExchange");
//       x-dead-letter-routing-key  这里声明当前队列的死信路由key
//        args.put("x-dead-letter-routing-key", "deadKey1");
//        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable("bizQueue1").withArguments(args).build();
    }
    //业务队列2
    @Bean
    public Queue bizQueue2(){
        //队列1和死信交换机绑定
        Map<String, Object> args = new HashMap<>();
//       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
//        args.put("x-dead-letter-exchange", "deadLetterExchange");
////       x-dead-letter-routing-key  这里声明当前队列的死信路由key
//        args.put("x-dead-letter-routing-key", "deadKey2");
//        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable("bizQueue2").withArguments(args).build();
    }
    //3.让交换机和队列绑定

    @Bean
    public Binding binding1(@Qualifier("bizQueue1") Queue queue, @Qualifier("bizExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean
    public Binding binding2(@Qualifier("bizQueue2") Queue queue, @Qualifier("bizExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    ////////////=======================以下是死信相关配置======================///////////////////////////////
    // 声明死信Exchange
    /*@Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
        return new DirectExchange("deadLetterExchange");
    }

    // 声明死信队列A
    @Bean("deadLetterQueue1")
    public Queue deadLetterQueueA(){
        return new Queue("deadLetterQueue1");
    }

    // 声明死信队列B
    @Bean("deadLetterQueue2")
    public Queue deadLetterQueueB(){
        return new Queue("deadLetterQueue2");
    }

    //绑定死信交换机和死信队列的关系
    // 声明死信队列A绑定关系
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueue1") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("deadKey1");
    }

    // 声明死信队列B绑定关系
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadLetterQueue2") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("deadKey2");
    }*/
}
